/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.InBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.InBill;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class InBillDaoImpl extends BaseDaoImpl implements InBillDao {
	public InBill get(String billCode) {
		String sql = "from com.minierp.model.InBill as obj where obj.billCode = :billCode and obj.enabled=\'Y\' order by obj.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (InBill) list.get(0);
	}

	public List<?> getList(String searchKey, boolean examined, long tsId,
			UserBean ub, Date allowStart, int startIndex, int num)
			throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName) or (b.businessBillCode like :businessBillCode))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.inDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "from com.minierp.model.InBill b where b.examined=:examined and b.enabled=\'Y\' "
				+ str + " order by b.id desc";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
				q.setString("businessBillCode", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public int getListCount(String searchKey, boolean examined, long tsId,
			UserBean ub, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName) or (b.businessBillCode like :businessBillCode))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.inDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.InBill b where b.examined=:examined and b.enabled=\'Y\' "
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
				q.setString("businessBillCode", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			List list = q.list();
			if (!list.isEmpty()) {
				int arg13 = Integer.parseInt(list.get(0).toString());
				return arg13;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List<?> getListForCancel(String searchKey, long tsId, UserBean ub,
			Date allowStart, int startIndex, int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName) or (b.businessBillCode like :businessBillCode))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.inDate>=:allowStart";
		}

		String sql = "from com.minierp.model.InBill b where b.enabled=\'Y\' "
				+ str + " order by b.id desc";
		Session sess = null;

		List arg12;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
				q.setString("businessBillCode", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg12 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg12;
	}

	public int getListForCancelCount(String searchKey, long tsId, UserBean ub,
			Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName) or (b.businessBillCode like :businessBillCode))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.inDate>=:allowStart";
		}

		String sql = "select count(b.id) from com.minierp.model.InBill b where b.enabled=\'Y\' "
				+ str;
		Session sess = null;

		int arg11;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
				q.setString("businessBillCode", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (list.isEmpty()) {
				return 0;
			}

			arg11 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updateTime,b.enabled,b.examined from com.minierp.model.InBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}

	public int estCorporate(final long fromClientId, final long toClientId,
			final String estType) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.InBill set offererId="
								+ toClientId
								+ " ,updateTime=:updateTime"
								+ " where offererId="
								+ fromClientId
								+ " and offererType=:offererType";
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						q.setString("offererType", estType);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getListByBusinessBill(String businessBillCode)
			throws Exception {
		String sql = "from com.minierp.model.InBill b where b.enabled=\'Y\'  and b.businessBillCode=:businessBillCode order by b.inDate";
		Session sess = null;

		List arg5;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("businessBillCode", businessBillCode);
			arg5 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg5;
	}
}